package com.lancoo.znbkxx.teacher.ui.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassEstimateGradeInfor
import com.lancoo.znbkxx.teacher.utils.ButtonUtils
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_estimate_grade.view.*

class AibkEstimateGradeAdapter (
        val activity: ComponentActivity?,
        val classEstimateGradeInfor: ClassEstimateGradeInfor,
        val maxDistreNum:Int
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_estimate_grade, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val distreNum = classEstimateGradeInfor.ScoreIndexMap[position].DistreNum
        val distreIndex = classEstimateGradeInfor.ScoreIndexMap[position].DistreIndex
        if(position == 0){
            holder.convertView.TvPeopleNum.setTextColor(Color.parseColor("#fa8d8d"))
            holder.convertView.IvGradePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_red_5)
        }else if(position == 1){
            holder.convertView.TvPeopleNum.setTextColor(Color.parseColor("#f9ad4f"))
            holder.convertView.IvGradePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_orange_5)
        }else if(position == 2){
            holder.convertView.TvPeopleNum.setTextColor(Color.parseColor("#f9ad4f"))
            holder.convertView.IvGradePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_orange_5)
        }else if(position == 3){
            holder.convertView.TvPeopleNum.setTextColor(Color.parseColor("#22d7bb"))
            holder.convertView.IvGradePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_green_5)
        }else if(position == 4){
            holder.convertView.TvPeopleNum.setTextColor(Color.parseColor("#00afff"))
            holder.convertView.IvGradePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_blue_5)
        }
        holder.convertView.TvPeopleNum.text = distreNum.toString() +"人"
        showIvData(holder.convertView.IvGradePeopleNum,distreNum, holder.convertView.TvGradeRange,distreIndex)

        holder.convertView.IvGradePeopleNum.setOnClickListener {
            //显示学生预估分数弹窗
            if (!ButtonUtils.isFastDoubleClick(R.id.IvGradePeopleNum)) {
                clickListener!!.onClick(position)
            }
        }

    }

    override fun getItemCount(): Int {
        return 5
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun showIvData(Iv: ImageView, data:Int,
                           Tv: TextView, typeName:String){

        val para1 = Iv.layoutParams
        para1.height = DensityUtils.dp2px(activity,(data*200f)/maxDistreNum)
        Iv.layoutParams = para1

        Tv.text = typeName
    }

    interface OnClickListener {
        fun onClick(pos:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}